//
//  MCActivityPageViewController.swift
//  StoreManage
//
//  Created by MC on 2018/6/29.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCActivityPageViewController: MCPageViewController {
    
    
    var navBarHairLineImageView : UIImageView?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        let bar = self.navigationController?.navigationBar
        let image = UIImage.init(color: UIColor.MCMain)
        bar?.setBackgroundImage(image, for: UIBarMetrics.default)
        navBarHairLineImageView?.isHidden = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        let bar = self.navigationController?.navigationBar
        bar?.setBackgroundImage(UIImage.init(named: ""), for: UIBarMetrics.default)
        navBarHairLineImageView?.isHidden = false
    }
    
    
    deinit {
        MCNotification_remove(vc: self, name: NOTIFICATION_ChangeStore)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        sendNetworking()
    }
    
    
    // MARK: - Setter & Getter
    lazy var indexTabIdArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(19)
        label.text = "营销活动"
        label.textAlignment = NSTextAlignment.center
        label.textColor = UIColor.white
        label.frame = CGRect.init(x: 0, y: 0, width: 100, height: 44)
        return label
    }()
    
    lazy var rightButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.setTitle("新增", for: .normal)
        button.frame = CGRect.init(x: 0, y: 0, width: 50, height: 40)
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.right
        button.setTitleColor(UIColor.white, for: .normal)
        button.addTarget(self, action: #selector(rightButtonClicked), for: .touchUpInside)
        return button
    }()
    
}


//MARK: 通知回调，闭包回调，点击事件
extension MCActivityPageViewController {
    // MARK: - Action
    @objc func rightButtonClicked() {
        let vc = MCActivityAddViewController()
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: false)
    }

    
    @objc func changeStoreEvent() {
        
        if MCUserinfo_getLevel() >= MCRole_manager {
            self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: rightButton)
        } else {
            self.navigationItem.rightBarButtonItem = nil
        }
        
        sendNetworking()
    }
}


//MARK: 网络请求
extension MCActivityPageViewController {
    
    // MARK: - Network
    func sendNetworking() {
        let para = [:] as [String : Any]
        
        
        MCNetwork.POST(JAVA_findMarketingTabs, para, vc: self, success: { (data) in
            self.indexTabIdArrayM.removeAllObjects()
            let dataArray = data.arrayObject ?? [Any]()
            
            for dict in dataArray {
                let model = MCActivityPageTabModel()
                model.setValuesForKeys(dict as? [String:Any] ?? [:])
                self.indexTabIdArrayM.add(model)
            }
            
            self.createPageViewController(tabArr: self.indexTabIdArrayM)
        }, failure: nil)
    }

}

//MARK: UI的处理
extension MCActivityPageViewController {
    // MARK: - Method
    func baseSetting() {
        self.navigationItem.titleView = titleLabel
        self.view.backgroundColor = UIColor.white
        navigationItem.leftBarButtonItem = nil
        
        navBarHairLineImageView = MCFindNavigationBarLine(view: (navigationController?.navigationBar)!)
        
        
        if MCUserinfo_getLevel() >= MCRole_manager {
            self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: rightButton)
        } else {
            self.navigationItem.rightBarButtonItem = nil
        }
        
        
        MCNotification_addObserver(name: NOTIFICATION_ChangeStore, vc: self, selector: #selector(changeStoreEvent))
    }
    
    
    func createPageViewController(tabArr : NSArray) {
        
        let tabArrayM = NSMutableArray.init(array: tabArr)
        
        let titleArrayM = NSMutableArray()
        let vcArrayM = NSMutableArray()
        
        
        for subModel in tabArrayM {
            let model = subModel as! MCActivityPageTabModel
            
            let tabName = model.tabName
            let activityType = model.activityType
            
            titleArrayM.add(tabName)
            
            switch (activityType) {
            case 8:  // 社区分销
                let vc = MCMarketingListViewController()
                vc.activityType = activityType.intValue
                vc.closure = { vc in
                    MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: false)
                }
                vcArrayM.add(vc)
            case 9:  // 优惠券
                let vc = MCTicketViewController()
                vc.activityType = activityType.intValue
                vc.closure = { vc in
                    MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: false)
                }
                vcArrayM.add(vc)
            default:
                break
            }
        }
        
        self.isLeftPosition = true
        self.initWithTitleArray(titleArrayM as! [Any], vcArray: vcArrayM as! [Any], blockNormalColor: UIColor.MCGray_middle, blockSelectedColor: UIColor.MCMain, currentPage: 0)
    }
}
